package cn.xgj.sfzy.controller;

import cn.xgj.sfzy.entity.Field;
import cn.xgj.sfzy.service.FieldService;
import cn.xgj.sfzy.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张卫姣
 * @Date 2024年10月24日 10:11
 * @Version 1.0
 */
@RestController
@RequestMapping("/field")
public class FieldController {
    @Autowired
    private FieldService fieldService;

    @PostMapping("/addField")
    public Result addField(@RequestBody Field field){
        Integer num = fieldService.add(field);
        if (num!=0){
            return Result.success("添加成功！");
        }else{
            return Result.error("添加失败！");
        }
    }

    @DeleteMapping("/removeField/{fieldId}")
    public Result removeField(@PathVariable("fieldId") Integer fieldId){
        Integer num = fieldService.remove(fieldId);
        if (num==0){
            return Result.error("删除失败！");
        }else if(num==-1){
            return Result.error("该类型相关数据未删除，请删除之后再试！");
        }
        else{
            return Result.success("删除成功！");
        }
    }

    @PutMapping("/editField")
    public Result editField(@RequestBody Field field){
        Integer num = fieldService.edit(field);
        if (num!=0){
            return Result.success("修改成功！");
        }else{
            return Result.error("修改失败！");
        }
    }

    @GetMapping("/findAllField")
    public Result findAllField(Field field){
        List<Field> fields = fieldService.findAll(field);
        return Result.success(fields);
    }

    @GetMapping("/findById/{fieldId}")
    public Result findById(@PathVariable("fieldId") Integer fieldId){
        Field field = fieldService.findById(fieldId);
        return Result.success(field);
    }

    @GetMapping("/findByPage")
    public Result findByPage(
            @RequestParam(required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(required = false,defaultValue = "5")Integer pageSize,Field field){

        PageInfo<Field> pageInfo = fieldService.findByPage(pageNum, pageSize, field);
        return Result.success(pageInfo);

    }

}
